//
//  ICChatMessageImageCell.m
//  ICome
//
//  Created by ENN on 16/3/13.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChatMessageImageCell.h"
#import "ICMediaManager.h"
#import "ICMessageModel.h"
#import "ICMessage.h"
#import "ICFileTool.h"
#import "ICHeaderFile.h"

@interface ICChatMessageImageCell ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIImage *oglImage;
@end

@implementation ICChatMessageImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.imageV];
    }
    return self;
}



#pragma mark - Private Method

- (void)setModelFrame:(ICMessageFrame *)modelFrame
{
    [super setModelFrame:modelFrame];
    
    self.imageV.frame = modelFrame.picViewF;
    self.bubbleView.userInteractionEnabled = _imageV.image != nil;
    self.bubbleView.image = nil;

    UIImage *placeholder = nil;
   
    if (![ICTools stringEmpty:modelFrame.model.mediaPath]) { // 本地存在图片 -- 针对自己发送的图片做特殊处理
        NSString *locImgPath = [[ICMediaManager sharedManager] imagePathWithName:[modelFrame.model.mediaPath lastPathComponent]];
        placeholder = [UIImage imageNamedFromiComeSDKBundle:locImgPath];//当加载时会缓存图片
      //  placeholder = [UIImage imageWithContentsOfFile:locImgPath];//仅加载图片

    } else {
        placeholder = [UIImage imageWithColor:[UIColor lightGrayColor]];
    }
   
    UIImage *arrowImage = [UIImage makeArrowImageWithSize:modelFrame.picViewF.size image:placeholder isSender:modelFrame.model.isSender];
    self.imageV.image = arrowImage;
   
    NSString *fileKey = modelFrame.model.message.fileKey;
    WEAKSELF
    [self.imageV setImageWithURL:[NSURL URLWithString:[ICTools getFileMaxImageURL:(fileKey)]]
                       placeholder:arrowImage
                         options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation
                          progress:nil
                         transform:^UIImage *(UIImage *image, NSURL *url) {
//                             if (!image) return nil;
//                             UIImage *arrowImage = [UIImage makeArrowImageWithSize:modelFrame.picViewF.size image:image isSender:modelFrame.model.isSender];
//                             return arrowImage;
                             return image;
                          }
                        completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                            if (!image) return;
                            weakSelf.oglImage = image;
                            UIImage *arrowImage = [UIImage makeArrowImageWithSize:modelFrame.picViewF.size image:image isSender:modelFrame.model.isSender];
                            weakSelf.imageV.image = arrowImage;
    }];
}

- (void)imageVClick
{
    if (self.oglImage == nil) {
        return;
    }
//    CGRect smallRect = [ICMessageHelper photoFramInWindow:btn];
//    CGRect bigRect   = [ICMessageHelper photoLargerInWindow:btn];
//    NSValue *smallValue = [NSValue valueWithCGRect:smallRect];
//    NSValue *bigValue   = [NSValue valueWithCGRect:bigRect];
    [self routerEventWithName:GXRouterEventImageTapEventName
                     userInfo:@{MessageKey   : self.modelFrame,
//                                @"smallRect" : smallValue,
//                                @"bigRect"   : bigValue,
                                @"obj"  : self.imageV,
                                @"oglImage" : self.oglImage,
                                @"currentCell":self
                                }];
}



#pragma mark - Getter

- (UIImageView *)imageV
{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        WEAKSELF
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf imageVClick];
        }];
        [_imageV addGestureRecognizer:tap];
        _imageV.userInteractionEnabled = YES;
//        [_imageV addTarget:self action:@selector(imageVClick:) forControlEvents:UIControlEventTouchUpInside];
//        _imageV.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        _imageV.layer.masksToBounds = YES;
        _imageV.layer.cornerRadius = 5;
        _imageV.clipsToBounds = YES;
    }
    return _imageV;
}





@end
